package com.itheima.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.entity.YddArticle;
import com.itheima.entity.YddCate;
import com.itheima.mapper.CateMapper;
import com.itheima.service.YddArticleService;
import com.itheima.service.YddCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class YddCateServiceImpl extends ServiceImpl<CateMapper, YddCate> implements YddCateService {
    @Autowired
    private YddArticleService articleService;

    @Override
    public void remove(Integer id) {
        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        //添加查询条件，根据分类id进行查询菜品数据
        LambdaQueryWrapper<YddArticle> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.eq(YddArticle::getCateid, id);
        int count1 = articleService.count(articleLambdaQueryWrapper);
        //如果已经关联，抛出一个业务异常
        if (count1 > 0) {
            throw new CustomException("当前分类下关联了文章，不能删除");//已经关联菜品，抛出一个业务异常
        }

     /*   //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2 > 0) {
            throw new CustomException("当前分类下关联了套餐，不能删除");//已经关联套餐，抛出一个业务异常
        }*/

        //正常删除分类
        super.removeById(id);
    }
}
